/*******************************************************************************
 * Package: com.hngtrust.dualrecord.user.service.impl
 * Type:    LoginSuccessServiceImpl
 * Date:    2019/11/06 14:37
 * 
 * Copyright (c) 2019 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 * 
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.user.service.impl;

import com.hngtrust.framework.id.service.MyIdGenerator;
import com.hngtrust.framework.security.api.LoginSuccessService;
import com.hngtrust.framework.security.entity.LoginUser;
import com.hngtrust.iir.base.entity.LogInfo;
import com.hngtrust.iir.base.entity.OptLog;
import com.hngtrust.iir.base.service.LogInfoService;
import com.hngtrust.iir.base.service.OptLogService;
import com.hngtrust.iir.constant.Constant;
import com.hngtrust.iir.user.entity.Department;
import com.hngtrust.iir.user.entity.Functions;
import com.hngtrust.iir.user.entity.RoleFunc;
import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.user.service.DepartmentService;
import com.hngtrust.iir.user.service.FunctionsService;
import com.hngtrust.iir.user.service.RoleFuncService;
import com.hngtrust.iir.user.service.UserService;
import com.hngtrust.iir.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Chentianyong
 */
@Service
public class LoginSuccessServiceImpl  implements LoginSuccessService{
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private DepartmentService departmentService;
    
    @Autowired
    private RoleFuncService roleFuncService;
    
    @Autowired
    private FunctionsService functionsService;
    
    @Autowired
    private MyIdGenerator myIdGenerator;
    
    @Autowired
    private LogInfoService logInfoService;
    
    @Autowired
    private OptLogService optLogService;
    
    @Override
    public Object loginSuccess(HttpServletRequest request, LoginUser loginUser) {
        String ip = HttpUtils.getIpAddressByCDN(request);
        
        User t = new User();
        t.setId(Long.valueOf(loginUser.getId()));
        List<User> list = userService.queryList(t);
        if(!list.isEmpty()) {
            User user = list.get(0);
            //检查状态
            if(!Constant.DEFAULT_STATUS_NORMAL.equals(user.getStatus())){
                return null;
            }
            //写日志
            OptLog req = new OptLog();
            Long logId = myIdGenerator.nextId();
            req.setId(logId);
            req.setUserId(user.getId());
            req.setUserCode(user.getUserCode());
            req.setUserName(user.getName());
            req.setIp(ip);
            req.setOptModule(Constant.LOG_OPTMODULE_LOGIN);
            req.setOptContent(user.getName()+"登录系统");
            req.setOptType(Constant.LOG_OPTTYPE_LOGIN);
            req.setStatus(Constant.LOG_OPTSTATE_SUCCESS);
            req.setCreateTime(new Date());
            optLogService.add(req);
            
            LogInfo logData = new LogInfo();
            logData.setId(myIdGenerator.nextId());
            logData.setRequestIp(ip);
            logData.setUserId(user.getId());
            logData.setRequestUrl("/iir/login");
            logData.setStartTime(new Date());
            logData.setCreateTime(new Date());
            logInfoService.add(logData);
            
            //查询权限
            List<RoleFunc> funcList = new ArrayList<>();
            if(user.getDeptId() > 0){
                Department dept = departmentService.queryById(user.getDeptId());
                if(dept.getDeptType().equals(Constant.DEPT_TYPE_SPECIAL)){ //特殊部门
                    List<Functions> functions = functionsService.queryList(new Functions());
                    for(Functions obj:functions){
                        RoleFunc rf = new RoleFunc();
                        rf.setFuncId(obj.getId());
                        funcList.add(rf);
                    }
                }
            }
            if(user.getRoleId() > 0 && funcList.size() <= 0){
                //查询功能权限
                RoleFunc userFunction = new RoleFunc();
                userFunction.setRoleId(user.getRoleId());
                funcList = roleFuncService.queryList(userFunction);
            }
            user.setFuncList(funcList);
            //组装返回数据
            user.setSid(request.getSession().getId());

            loginUser.setAdditionalInfo(user);
            
            return user;
        } 
        return null;
    }
    
}
